/**
 * Copyright: Copyright (c) 2018, 2022
 * Company: 杭州叙简科技股份有限公司
 */
package cn.com.scooper.controller;

import java.util.*;

import cn.com.scooper.common.config.ConfigKit;
import cn.com.scooper.common.constant.CommonConstant;
import cn.com.scooper.common.resp.APIRespJson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import cn.com.scooper.common.resp.APIObjectJson;

/**
 * 配置文件相关
 * @author LuLihong
 * @date 2019-12-03
 */
@Controller
@RequestMapping("/config")
public class ConfigController {
	private static final String CONFIG_KEY_SKIN = "skin";

	@RequestMapping("/getConfigByKey")
	public APIRespJson getConfigByKey(@RequestParam String key) {
		String value = ConfigKit.find(key, "");
		Map<String, String> configMap = new HashMap<>(1);
		configMap.put(key, value);

		return new APIObjectJson(configMap);
	}

	@RequestMapping("/getConfigByKeys")
	public APIObjectJson getConfigByKeys(@RequestParam List<String> keys) {
		Map<String, String> configMap = new HashMap<>(keys.size());
		for (String key: keys) {
			String value = ConfigKit.find(key, "");
			configMap.put(key, value);
		}

		return new APIObjectJson(configMap);
	}

	/**
	 * 根据前缀加载配置项，该前缀配合.一起组成前缀去配置库中加载匹配的属性
	 * @param prefix
	 * @return
	 */
	@RequestMapping("/getConfigByPrefix")
	public APIObjectJson getConfigByPrefix(@RequestParam String prefix) {
		Map<String, String> configMap = Optional.ofNullable(ConfigKit.getByPrefix(prefix))
				.orElse(Collections.emptyMap());

		return new APIObjectJson(configMap);
	}

	/**
	 * 加载主题
	 * @return
	 */
	@RequestMapping("/getSkin")
	public APIObjectJson getSkin() {
		Map<String, String> configMap = new HashMap<>(1);
		String skin = ConfigKit.find(CONFIG_KEY_SKIN, CommonConstant.SKIN_DEFAULT);
		configMap.put(CONFIG_KEY_SKIN, skin);

		return new APIObjectJson(configMap);
	}

}
